package com.common.response;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @description:
 * @author: zyj
 * @create: 2019-12-05 11:45
 **/
public abstract class BaseResponseController implements BaseResponse {

    @Override
    public ResponseBean error(){
        return errorCodeMsg(ResponseCode.REQUEST_ERROR.getValue(), ResponseCode.REQUEST_ERROR.getDescripe());
    }

    @Override
    public ResponseBean errorCode(String code){
        return errorCodeMsg(code, ResponseCode.REQUEST_ERROR.getDescripe());
    }

    @Override
    public ResponseBean errorCodeMsg(String code, String message){
        ResponseBean responseBean = new ResponseBean();
        responseBean.setCode(code);
        responseBean.setMessage(message);

        return responseBean;
    }

    @Override
    public ResponseBean successCodeMsgData(String code, String message, Object data){
        ResponseBean responseBean = new ResponseBean();
        responseBean.setCode(code);
        responseBean.setMessage(message);
        responseBean.setData(data);

        return responseBean;
    }

    @Override
    public ResponseBean successCodeMsg(String code, String message){
        return successCodeMsgData(code, message, null);
    }

    @Override
    public ResponseBean successCode(String code){
        return successCodeMsgData(code, ResponseCode.REQUEST_SUCCESS.getDescripe(), null);
    }

    @Override
    public ResponseBean success(){
        return successCodeMsgData(ResponseCode.REQUEST_SUCCESS.getValue(), ResponseCode.REQUEST_SUCCESS.getDescripe(), null);
    }

    public ResponseBean successData(Object data){
        return successCodeMsgData(ResponseCode.REQUEST_SUCCESS.getValue(), ResponseCode.REQUEST_SUCCESS.getDescripe(), data);
    }

    public ResponseBean successMsg(String message){
        return successCodeMsgData(ResponseCode.REQUEST_SUCCESS.getValue(), message, null);
    }


    public HttpServletResponse getResponse(){
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = servletRequestAttributes.getRequest();
        HttpServletResponse response = servletRequestAttributes.getResponse();
        return response;
    }

    public HttpServletRequest getRequest(){
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = servletRequestAttributes.getRequest();
        return request;
    }

}
